package dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;

import beans.Restaurant;
import beans.UserRestaurantOwner;
public class UserRestaurantOwnerDAO {

private HibernateTemplate hibernateTemplate;
private Transaction transaction;	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
		this.transaction = sessionFactory.openSession().beginTransaction();
	}
	
	public void saveUserRestaurantOwner( UserRestaurantOwner userRestaurantOwner){
		hibernateTemplate.save(userRestaurantOwner);
		transaction.commit();
	}
	public List<UserRestaurantOwner> GetAllUserRestaurantOwners(){
		return hibernateTemplate.loadAll(UserRestaurantOwner.class);
	}
	public UserRestaurantOwner findUserRestaurantOwnerbyUsername(String user_username){
		UserRestaurantOwner userRestaurantOwner = new UserRestaurantOwner();
		for(UserRestaurantOwner x: GetAllUserRestaurantOwners()){
			if(x.getUsername().equalsIgnoreCase(user_username)){
				userRestaurantOwner.setId(x.getId());
				userRestaurantOwner.setUsername(x.getUsername());
				userRestaurantOwner.setRestaurant_id(x.getRestaurant_id());
			}
		}
		return userRestaurantOwner;
	}
	public UserRestaurantOwner findUserRestaurantOwnerbyId(int id){
		UserRestaurantOwner userRestaurantOwner = new UserRestaurantOwner();
		for(UserRestaurantOwner x: GetAllUserRestaurantOwners()){
			if(x.getId()==id){
				userRestaurantOwner.setId(x.getId());
				userRestaurantOwner.setUsername(x.getUsername());
				userRestaurantOwner.setRestaurant_id(x.getRestaurant_id());
			}
		}
		return userRestaurantOwner;
	}
}
